<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ include file="/commons/taglibs.jsp"%>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en-us">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrom=1">
	<meta http-equiv="Cache-Control" content="no-store"/>
	<meta http-equiv="Pragma" content="no-cache"/>
	<meta http-equiv="Expires" content="0"/>
 	<%@ include file="../base.jsp" %>
	<title>修改</title>
	
	<script type="text/javascript">
		var trId=-1;
	
		$(function(){
			/**
			**下拉框样式
			**/
 			$(".chzn-select-deselect").chosen({allow_single_deselect:true});
			$(".chzn-select-nosearch").chosen({disable_search:true});
			$(".chzn-select").chosen();
			var zidx = 100;
			$('.chzn-container').each(function(){
			$(this).css('z-index', zidx);
			zidx-=1;
			});
			//下拉框样式end
			
			/**开关按钮**/
			// 1 单一  2复合 
	        $("#complex_switch").bootstrapSwitch();
	        $("#complex").val("1");
	    	$("#complex_switch").bootstrapSwitch('state',true, true);
		    $("#complex_switch").on('switchChange.bootstrapSwitch', function(event, state) {
		        	if (state==true){
		        	    $("#complex").val("1");  
		        	}else{
		        		$("#complex").val("2"); 
		        	}
		        });
		    /**开关按钮end**/
			
 
		});
	
		function addTr(){
			$.ajax({
		 	   	  type:"post",
		 	   	  url:"${ctx}/sell/getMedical.do",
		 	   	  data:{medicalId:$("select[id=medicalSelect]").val()},
		 	   	  success:function(data){
		 	   		var json = eval('('+data+')')
						var html='<tr id="'+json.id+'" onclick="getTR('+json.id+')" >'+
							'<td>'+json.number+'</td>'+
							'<td>'+json.name+' &nbsp;&nbsp;&nbsp;<span id="'+json.id+'"  name="showMessage" style="color: red" ></span> </td>'+
							'<td><input type="hidden" name="medicalId"  value="'+json.id+'" ><input type="text" id="'+json.id+'" name="price" value="'+json.price+'" readonly="readonly" ></td>'+
							'<td><input type="text" id="'+json.id+'" name="quantity" onchange="calculation('+json.id+')" class="easyui-numberbox" data-options="required:true,min:0,precision:2"   >'+
							'<input type="hidden" id="'+json.id+'" name="amount" value="'+json.amount+'" >'+
							'<input type="hidden" name="oldquantity"  id="'+json.id+'" value="0" >'+
							'<input type="hidden" id="'+json.id+'" name="cost" value="'+json.cost+'" ></td> '+
							'<td><input type="hidden" id="'+json.id+'" name="total"  > <span total-id="'+json.id+'"></span> </td>'+
						'</tr>';
						$("#table").append(html);
						
		 	   	  }
		 	});
		}
		
		function calculation(id){
			var quantity=parseFloat($("input[name=quantity][id="+id+"]").val());
			var oldquantity=parseFloat($("input[name=oldquantity][id="+id+"]").val());
			var div=quantity-oldquantity;
			var amount=parseFloat($("input[name=amount][id="+id+"]").val());
			var price=parseFloat($("input[name=price][id="+id+"]").val());
			if(div<=amount){
				var count=quantity*price;
				count=rounding(count,2)
				$("input[name=total][id="+id+"]").val(count)
				$("span[total-id="+id+"]").text(count);
				$("span[id="+id+"][name=showMessage]").text("");
				
			}else{
				$("span[id="+id+"][name=showMessage]").text("该药品数量不足只有："+$("input[name=amount][id="+id+"]").val());
				//setTimeout("deleteTR("+id+")",3000);
				//setTimeout('$("#showMessage").text("")',3000);
			}
		}
		
		function getTR(id){
			trId=id;
		}
		function deleteTR(){
			$("tr[id="+trId+"]").remove();
		}
		
	    function subdata(){
	    	var count=0;
	    	$("input[name=total]").each(function(){
	    		count+=parseFloat( $(this).val() );
	    	});
	    	count=rounding(count,2)
	    	$("input[name=amount]").val(count);
	    	$("input[id=amount]").val(count);
	    	
	    	 
			debugger;
	    	/* if($("span[id=number]").text()!="") return; */
	         if(!$("#signupForm").form('validate')){
	            return false;
	        }else{
	        	$("#submitButton").attr("disabled","true").addClass("grayButton");
	    		$("#signupForm").submit();
	        }
	    }
	    
	  //四舍五入
	    function rounding(value,digit){
	    	 var re= new Number(value).toFixed(digit);
	    	 if( ''+re+''=='NaN'){
	    		 re=0;
	    		}
	    	 return re;
	    }
	   
	    function doReturn(){
			if(confirm("数据将不会被保存！")){
				//如果是true，页面返回index页面
				location.href="${ctx}/sell/list.do";
			}else{
				//如果是false，页面不做处理
			}
		}
	</script>
</head>
<body>
  <div class="con_wrap">
	 <%@ include file="../head.jsp" %>
	 <div id="main">
		  <form id="signupForm" style="display: inline;" autocomplete="off" action="${ctx}/sell/update.do" method="post" >
				<div id="main">
					<dl class="section_wrap">
						<dt>
							<h2>编辑</h2></dt>
						<dd class="dd">
							<table class="form">
								<tr>
									<td class="tit">客　户：</td>
									<td class="SELECT">
									<input type="hidden" name="cId" id="cId" value="${collection.id}">
									<select name="clientId" id="clientId"  
										class="chzn-select-deselect w20" data-placeholder="Choose a member...">
											<option value=""></option>
											<c:forEach items="${memberList}" var="member" varStatus="v">
												<option value="${member.id}"  <c:if test="${member.id==collection.clientId}"> selected </c:if>         >${member.cardId}</option>
											</c:forEach>
									</select></td>
									<td class="tit">类　型：</td>
									<td class="SELECT">
										<select name="payType" id="payType"
											class="chzn-select-deselect w20" data-placeholder="Choose a pay....">
												<option value=""></option>
												<c:forEach items="${payTypeList}" var="p">
													<option value="${p.id}"  <c:if test="${p.id==collection.payType}"> selected </c:if> >${p.paymethod}</option>
												</c:forEach>
										</select>
									</td>
											
								</tr>
								<tr>
									<td class="tit">金　额：</td>
									<td > 
										<input type="text" name="amount"  id="amount" class="easyui-numberbox" value="${collection.amount}"
										data-options="required:true,min:0,precision:2" />
										<input type="hidden" name="mcost"  id="mcost" />
									</td>
								</tr>
 
								<tr>
									<td colspan="4">
										<span style="color: red">${mes}</span>
									</td>
								</tr>
							</table>
						</dd>
						<dd class="dd">
						<div>
							 
							<select onchange="addTr()" id="medicalSelect"   class="chzn-select-deselect w20" style="width: 200px;" data-placeholder="Choose a medical...">
									<option value=""></option>
									<c:forEach items="${medicalList}" var="medical" varStatus="v">
										<option value="${medical.id}">${medical.number}</option>
									</c:forEach>
							</select> 
							 <span onclick="deleteTR()">
								<span class="glyphicon glyphicon-minus" aria-hidden="true"></span>
							</span>
							 
							 
								<table class="grid" id="date">
									<thead>
										<tr>
											<th style="text-align: center;">编　号</th>
											<th style="text-align: center;">名　称</th>
											<th style="text-align: center;">售　价</th>
											<th style="text-align: center;">数　量</th>
											<th style="text-align: center;">小　计</th>
										</tr>
									</thead>
									<tbody id="table">
										<c:forEach items="${collectionSubList}" var="collectionSub" >
										
											<tr id="${collectionSub.medicalId}" onclick="getTR(${collectionSub.medicalId})" >
												<c:forEach items="${medicalList2}" var="medical">
													<c:if test="${medical.id==collectionSub.medicalId}">
														<td>${medical.number}</td>
														<td>${medical.name}&nbsp;&nbsp;&nbsp;<span id="${collectionSub.medicalId}"  name="showMessage" style="color: red" ></span>
															<input type="hidden" id="${collectionSub.medicalId}" name="amount" value="${medical.amount}" >
															<input type="hidden" id="${collectionSub.medicalId}" name="cost" value="${medical.mcost}" >
														</td>
													</c:if>
												</c:forEach>
											
												
												<td>
													<input type="hidden" name="id"  value="${collectionSub.id}" >
													<input type="hidden" name="medicalId"  value="${collectionSub.medicalId}" >
													<input type="hidden" name="collectionId"  value="${collectionSub.collectionId}" >
													<input type="text" id="${collectionSub.medicalId}" name="price" value="${collectionSub.price}" readonly="readonly" >
												</td>
												<td>
													<input type="hidden" name="oldquantity"  id="${collectionSub.medicalId}" value="${collectionSub.quantity}" >
													<input type="text" name="quantity"  id="${collectionSub.medicalId}" value="${collectionSub.quantity}" 
													onchange="calculation(${collectionSub.medicalId})"  >
												 </td> 
												<td>
													 <input type="hidden" id="${collectionSub.medicalId}" name="total"  value="${collectionSub.total}"  >
													 <span total-id="${collectionSub.medicalId}">${collectionSub.total}</span>
												 </td>
											</tr>
										</c:forEach>
									</tbody>
								</table>
								<table class="grid">
									<tr>
										<td> 
										</td>
										<td>
											&nbsp;&nbsp;&nbsp;
										</td>
										<td>
											&nbsp;
										</td>
										<td>
											&nbsp;
										</td>
										<td>
											&nbsp;
										</td>
										<td>
			 
			 								<input id="submitButton" name="submitButton" type="button" class="button blue" onclick="subdata()" value="保存" />
											<input type="button" class="button grey" onclick="doReturn();" value="取消"></input> 
										 
										</td>
									</tr>
								</table>
								 
							</div>
						</dd>
					</dl>
				</div>
			</form>
	</div>
<div style="height: 200px;">
</div>
	<div id="footer">
		<%@ include file="../footer.jsp" %>
	</div>
</div>
</body>
</html>	